<?php
include_once('../../php/postgre.php');

$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];

if ($act=='simpan_berita'){
	$idberita = $_POST['id_berita'];
	$judul = $_POST['judul'];
	$pengantar = $_POST['pengantar'];
	$isi = $_POST['isi'];
	
	if (!is_numeric ($idberita)){
		echo "Err: invalid id";
		exit;
	}
	if ($idberita>0){
		exec_query("update mus_berita set judul='".$judul."', pengantar='".$pengantar."', isi='".$isi."' where id_berita=".$id_berita);
		echo 'success__';
		
		logs('Mengedit berita','Judul berita : '.$judul);
	}else{
		$maxid = get_maxid ('id_berita', 'mus_berita');
		exec_query("insert into mus_berita(id_berita, judul, pengantar, isi) values(".$maxid.", '".$judul."', '".$pengantar."', '".$isi."')");
		$stored = get_data ('select id_berita from mus_berita where id_berita='.$maxid);
		if($stored['id_berita']==$maxid){
			echo 'success__';
			$idberita = $maxid;
			logs('Menambah berita','Judul berita : '.$judul);
		}
	}
	
	echo $idberita.'__';
	view_berita();
	
}else if($act=='del_berita'){
	$idberita=$_POST['idberita'];
	if(is_numeric($idberita)){
		$brt = get_data('select judul from mus_berita where id_berita='.$idberita);
		exec_query("delete from mus_berita where id_berita=".$idberita);
		echo 'success__';
		view_berita();
		
		logs('Menghapus berita','Judul berita : '.$brt['judul']);
		
	}else{
		echo 'Error';
	}
	
}else if($act=='edit_berita'){
	$idberita=$_POST['idberita'];
	if(is_numeric($idberita)){
		$data = get_data("select * from mus_berita where id_berita=".$idberita);
		$ress = implode($data,'__');
		echo $ress;
	}else{
		echo 'Error';
	}
}
function view_berita(){
	$x=1;
			$berita = get_datas("select * from mus_berita order by id_berita");
			foreach($berita as $berita){
			?>
            <tr>
				<td><?php echo $x ?></td>
            	<td><?php echo $berita['judul']?></td>
                <td><?php echo $berita['pengantar']?></td>
                <td><?php echo $berita['isi']?></td>
				<td style="text-align:center;">
					<a href="#modalwin" data-toggle="modal" class="btn btn-info btn-small bt-edit-program" name="<?php echo $berita['id_berita']; ?>"><i class="icon-edit icon-white"></i>Ubah</a>
                    <a class="btn btn-danger btn-small bt-delete-berita" name="<?php echo $berita['id_berita']; ?>"><i class="icon-trash icon-white"></i>Hapus</a>
					</td>
            </tr>
            <?php
			$x++;
}
}
?>